public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    @Override
    public void addFirst(int data) {
         ListNode node =new ListNode(data);
         node.next=head;
         head=node;
    }

    @Override
    public void addLast(int data) {
        ListNode node =new ListNode(data);
        ListNode cur =head;
        if (head==null){
            head=node;
            return;
        }
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node =new ListNode(data);
        ListNode cur=head;
        int len=size();
        try{
            insert(index,len);
        }catch(InsertpositionException e){
            e.printStackTrace();
        }
        if (index==1){
            addFirst(data);
            return;
        }
        if (index==len+1){
            addLast(data);
            return;
        }
        while(index-1!=1){
            index--;
            cur=cur.next;
        }
        ListNode curN=cur.next;
        cur.next=node;
        node.next=curN;
    }

    private void insert(int index,int len) throws InsertpositionException{
        if (index<1||index>len+1){
            throw new InsertpositionException("插入位置不合法！");
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if (cur.val==key)
                return true;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head==null)
            return;
        ListNode cur=head;
        if (head.val==key){
            head=head.next;
            return;
        }
        while(cur.next!=null){
            if (cur.next.val==key){
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
        System.out.println("链表中没有该值！");
    }

    @Override
    public void removeAllKey(int key) {
        if (head==null)
            return;
        ListNode p=head;
        ListNode cur=p.next;
        while(cur!=null){
            if (cur.val==key){
                p.next=cur.next;
            }else {
                p=p.next;
            }
            cur=cur.next;
        }
        if (head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int len=0;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        head=null;
    }

    @Override
    public void display() {
        ListNode cur =head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public boolean chkpalindrome(){ //链表的回文判断
        ListNode fast=head;
        ListNode slow=head;
        if (head==null)
            return false;

        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode cur=slow.next;
        while(cur!=null){
            ListNode lin=cur.next;
            cur.next=slow;
            slow=cur;
            cur=lin;
        }
        int len=0;
        while(head!=slow){
            if (head.val!=slow.val)
                return false;
            if(head.next==slow)
                return true;

            head=head.next;
            slow=slow.next;
        }
        return true;
    }
}
